const express = require('express');
let app = express();
let cors = require("cors")
const expressJWT = require('express-jwt')

app.use(cors());

app.listen(3000, () => {
    console.log("is 3000");
})
// 处理post参数中间件
app.use(express.urlencoded({
    extended: false
}));

// 处理token中间件
app.use(expressJWT({
    secret: 'be139',
    algorithms: ['HS256']
}).unless({
    path: ['/user/login', '/user/reg']
}))


// ************************************************************************ 设计一级路由

// 用户
let userRouter = require("./routers/user.js")
app.use("/user", userRouter);
// 日志
let todoRouter = require("./routers/todo.js")
app.use("/todo", todoRouter);

// 处理中间件问题
app.use(function (err, req, res, next) {
    if (err.name == "UnauthorizedError") {
        res.send({
            status: 1,
            message: "身份认证失败！"
        });
    }
});